package top.withwings.bonus.bonus.pojo.bonus.rule;

import org.springframework.dao.DataIntegrityViolationException;

import java.util.LinkedHashMap;

public class BonusRuleTypeCaster {

    private BonusRuleTypeCaster() {

    }

    public static BonusRule castToSubClass(BonusRule parent) {


        int id = parent.getId();
        String name = parent.getName();
        String type = parent.getType();
        String description = parent.getDescription();
        LinkedHashMap<String, Double> paramMap =  parent.getParamMap();

        if (type.equals(HighPriceBonusRule.TYPE)) {
            BonusRule rule = new HighPriceBonusRule(id, name, paramMap);
            rule.setDescription(description);
            return rule;
        } else if (type.equals(OnSaleBonusRule.TYPE)) {
            BonusRule rule = new OnSaleBonusRule(id, name, paramMap);
            rule.setDescription(description);
            return rule;
        } else if (type.equals(ExpirationCloseBonusRule.TYPE)) {
            BonusRule rule = new ExpirationCloseBonusRule(id, name, paramMap);
            rule.setDescription(description);
            return rule;
        } else if (type.equals(GrossAmountBonusRule.TYPE)) {
            BonusRule rule = new GrossAmountBonusRule(id, name, paramMap);
            rule.setDescription(description);
            return rule;
        } else {
            throw new DataIntegrityViolationException("未知的提成类型");
        }

    }


}